library(tidyverse)
library(sf)
library(leaflet)
library(htmltools)
library(htmlwidgets)
library(RColorBrewer)
First, I will import a set of points representing visited/resided buildings of probable/confirmed COVID-19 cases in the past 14 days (28 days if with cluster). I will convert the coordination system to WGS84.
covid <- st_read("CHP_BUILDING_COVID19_20201023.geojson")
## Reading layer `CHP_BUILDING_COVID19_20201023' from data source `E:\Github\mengyao_li-vis\CHP_BUILDING_COVID19_20201023.geojson' using driver `GeoJSON'
## Simple feature collection with 107 features and 12 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: 809968 ymin: 809225 xmax: 846100 ymax: 840233
## projected CRS: Hong Kong 1980 Grid System
covid <- covid %>%
st_transform(crs = 4326)
I will also import a set of polygons representing all districts of Hong Kong with their populations density information.
districts <- read_sf(dsn = ".", layer = "Hong_Kong_Mid_Year_Population_Density_in_2018")
names(districts)[5] <- "pop_density"
Now I will draw the points on a leaflet map so we can pan around and zoom in on the parts of Hong Kong where we want to see the buildings in more detail.
I will start by showing the points as markers. It looks less messy after zooming in.
covid_map1 <- leaflet(districts) %>%
addProviderTiles(providers$OpenStreetMap) %>%
addPolygons(fillColor = "grey", stroke = FALSE) %>%
addMarkers(data = covid,
popup = ~BLDGNAME_EN) %>%
addControl("Visited/resided buildings of probable/confirmed COVID-19 cases in the past 14 days", position = "topright") %>%
addControl('<a href = "https://geodata.gov.hk/gs/view-dataset?uuid=ab290617-f93b-416e-a00d-114566a7783a&sidx=0">Data source</a>',
position = "bottomleft")
covid_map1
It might look cleaner to show the points as little circles. I have learned how to cluster points from Jiwon.
pal <- colorNumeric(
palette = "YlOrRd",
domain = districts$pop_density)
covid_map2 <- leaflet(districts,
options = leafletOptions(minZoom = 11, maxZoom = 15)) %>%
addProviderTiles("Esri.WorldGrayCanvas") %>%
addPolygons(color = ~pal(pop_density), stroke = FALSE, fillOpacity = 0.3,
highlightOptions = highlightOptions(fillColor = "black", fillOpacity = 0.5),
label = districts$HKDistri_1,
popup = paste("Population Density: ", format(districts$pop_density, big.mark = ","), "persons per sq.km")) %>%
addCircleMarkers(data =covid,
fillColor = "red", color = "red",
stroke = FALSE, radius = 20,
label = "COVID-19 has been here",
popup = paste("Building Name: ", covid$BLDGNAME_EN, "<br/>",
"Related Case Number: ", covid$RELATEDCASESNO),
clusterOptions = markerClusterOptions()) %>%
setMaxBounds(lng1 = 113,
lat1 = 22,
lng2 = 115,
lat2 = 23) %>%
addControl("Visited/resided buildings of probable/confirmed COVID-19 cases in the past 14 days", position = "topright") %>%
addControl('<a href = "https://geodata.gov.hk/gs/view-dataset?uuid=ab290617-f93b-416e-a00d-114566a7783a&sidx=0">Data source</a>',
position = "bottomleft")
covid_map2
I can use the saveWidget() function to save any of my leaflet maps as in its own html file.
saveWidget(covid_map2, file = "covid buildings.html")